﻿using System;

namespace RexToy.UI.Controls
{
    public class Table : Control
    {
        public static Table Create(uint row, uint col)
        {
            var t = new RexToy.UI.Controls.Table();
            for (var i = 0; i < row; i++)
            {
                var r = t.InsertRow(0);
                for (var j = 0; j < col; j++)
                {
                    r.InsertCell(0);
                }
            }
            return t;
        }

        private TableRow[] __rows;
        public TableRow[] Rows
        {
            get { return __rows; }
        }

        internal Table()
        {
            this.__dragEventBubble = false;
            this.__domNode = RexToy.UI.Controls.DomNode.Create(new DomNodeCreateOptions()
            {
                tag = Tag.Table,
                cellPadding = "0",
                cellSpacing = "0",
                className = "ui-table",
                border = "0"
            });
            __rows = new TableRow[0];
        }

        public TableRow GetRow(uint idx)
        {
            return this.__rows[idx];
        }

        public uint GetRowCount()
        {
            var t = this.__domNode._element as System.Html.DOM.Table;
            return t.Rows.Length;
        }

        public TableRow InsertRow(uint idx)
        {
            var t = this.__domNode._element as System.Html.DOM.Table;
            var rowDom = t.InsertRow(idx);
            var row = new RexToy.UI.Controls.TableRow(rowDom);
            this.__rows.InsertAt(row, idx);
            return row;
        }

        public void DeleteRow(uint idx)
        {
            var t = this.__domNode._element as System.Html.DOM.Table;
            t.DeleteRow(idx);
            this.__rows.RemoveAt(idx);
        }

        public TableCell GetCell(uint row, uint col)
        {
            return this.GetRow(row).GetCell(col);
        }
    }
}
